package com.example.home.park;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.home.R;
import com.example.home.bean.CarParkBean;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static com.example.home.tool.StaticActi.ip;

public class CarParkDetailPageActivity extends AppCompatActivity {

    private OkHttpClient okHttpClient;
    private Gson gson;
    private Handler handler;
    private ImageView back;
    private TextView catf_name;
    private TextView cat_adress;
    private TextView cat_distance;
    private TextView cat_state;
    private TextView cat_cost;
    private TextView cat_residue;
    private TextView cat_reference;
    private Request request;
    private CarParkBean carParkBean;
    private Button more;

    private String listID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_park_detail_page);
        Intent intent = getIntent();
        listID = intent.getStringExtra("listID");
        initview();
        sendbean();
    }

    private void sendbean() {
        request = new Request.Builder()
                .url(ip+"/prod-api/api/park/lot/"+listID)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json = response.body().string();
                carParkBean = gson.fromJson(json, CarParkBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        bindData();
                    }
                });
            }
        });
    }

    private void bindData() {
        catf_name.setText("停车场名称:"+carParkBean.getData().getParkName());
        cat_adress.setText("地址:"+carParkBean.getData().getAddress());
        if (carParkBean.getData().getOpen().equals("Y")){
            cat_distance.setText("是否对外开放:是");
        }else if (carParkBean.getData().getOpen().equals("N")){
            cat_distance.setText("是否对外开放:否");
        }
        cat_state.setText("剩余车位:"+carParkBean.getData().getVacancy());
        cat_cost.setText("收费参考:每小时"+carParkBean.getData().getPriceCaps()+"元/天");
        cat_residue.setText("距离:"+carParkBean.getData().getDistance());
        cat_reference.setText("停车费:"+carParkBean.getData().getRates()+"元/小时");
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void initview() {
        okHttpClient = new OkHttpClient();
        gson = new Gson();
        handler = new Handler();
        back = findViewById(R.id.back);
        catf_name = findViewById(R.id.cat_name);
        cat_adress = findViewById(R.id.cat_adressa);
        cat_distance = findViewById(R.id.cat_distance);
        cat_state = findViewById(R.id.cat_state);
        cat_cost = findViewById(R.id.cat_cost);
        cat_residue = findViewById(R.id.cat_residue);
        cat_reference = findViewById(R.id.cat_reference);
        more =findViewById(R.id.more);
    }

}